import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components';
import './aboutus.scss';
import { get as getGlobalData } from '../../globaldata'
import configs from '../../configuration/index'
import {request_good} from "../../untils/request";

export default class Index extends Component {

  config = {
    navigationBarTitleText: '关于我们'
  };

  constructor(props) {
    super(props);
    this.state = {
      pay: {
        addorder: false,
        orderno: 'svr1811301246373de9f3',
        sorder_no:'尾单号码'
      }
    }
  }

  componentWillMount() {
    console.log(Taro.ENV_TYPE)
    console.log(Taro.getEnv())
    var result = getGlobalData('test')
    console.log(result)
  }

  componentDidMount() {}

  componentWillUnmount() {}

  componentDidShow() {}

  componentDidHide() {}

  paytest() {
    var self = this;
    var env = Taro.getEnv().toUpperCase()
    var token = Taro.getStorageSync('access_token')
    //此处判断尾单还是订单，并进行相关配置

    if(env === 'WEB'){
      //web支付调用
      let info = {
        orderno: self.state.pay.orderno,
        type: 'alipay ',
        return_url:'www.baidu.com',
      }
      let search_data = {
        url: configs.apis['webpay'],
        data: info,
        header: {
          'Authorization': 'Bearer ' + token
        }
      }
      request_good(search_data).then(res => {
        /*self.setState({ items: res.res.items });*/
        console.log(res);
        if(Number(res.code) === 0){
          let alipay_url = res.res.paydata.pay_url;
          window.location.href = alipay_url;
        }
      });
    }else {
      //小程序支付调用
      var pay_data = {};;
      if( env === 'SWAN' ){
        pay_data = {
          type: 'alipay',
          orderno:self.state.pay.orderno,
          //pay_way: 'requestAliPayment'
        }
      }else if(env === 'WEAPP') { //requestPayment
        let openid = Taro.getStorageSync('open_id')
        pay_data = {
          type: 'xcx_wxpay',
          orderno:self.state.pay.orderno,
          openid
        }
      }//wxpay和alipay待添加

      var url = configs.apis['@order/payment'];
      var ordernumber = self.state.pay.orderno;
      var search_data = {
        url,data:pay_data,header: {
          'Authorization': 'Bearer ' + token
        }
      }

      request_good(search_data).then(res => {
        console.log(res)//requestPayment ,requestPolymerPayment
        if (res.code === 0){
          if( env === 'WEAPP' ){
            Taro.requestPayment({
              ...res.res.paydata
            }).then(res => {
              console.log('成功',res)
              Taro.showToast({
                title: '支付成功',
                icon: 'success'
              });
              Taro.navigateTo({
                url: '/pages/pay-success/pay-success?orderno=' + ordernumber
              });
            }).catch(res => {
              console.log('失败',res)
              Taro.showToast({
                title: '支付失败'
              });
              Taro.navigateTo({
                url: '/pages/pay-failed/pay-failed'
              });
            });
          }else if(env === 'SWAN'){
            swan.requestAliPayment({
              orderInfo: res.res.paydata.orderString,
              success: function (res) {
                Taro.showToast({
                  title: '支付成功',
                  icon: 'success'
                });
                Taro.navigateTo({
                  url: '/pages/pay-success/pay-success?orderno=' + ordernumber
                });
              },
              fail: function (err) {
                Taro.showToast({
                  title: "支付失败"
                });
                Taro.navigateTo({
                  url: '/pages/pay-failed/pay-failed'
                });
              }
            })
          }

        }else {
          Taro.showToast({
            title: '创建支付订单失败',
            icon: 'none'
          });
        }
      }).catch(err =>{
        Taro.showToast({
          title: '创建支付订单失败',
          icon: 'none'
        });
        console.log('错误码：' + err);
      })

    }
  }

  render() {

    return <View className="aboutus" onClick={this.paytest.bind(this)}>
        关于我们
      </View>;
  }
}
